package com.taxer.service.impl;

import com.taxer.db.dao.AdminMapper;
import com.taxer.db.dao.EmpMapper;
import com.taxer.db.entity.Admin;
import com.taxer.db.entity.Emp;
import com.taxer.exception.TaxerExcepiton;
import com.taxer.service.EmpService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service("empService")
public class EmpServiceImpl implements EmpService{
    @Resource
    private EmpMapper empMapper;
    @Resource
    private AdminMapper adminMapper;

    @Override
    public int deleteByPrimaryKey(Long id) throws TaxerExcepiton {
        try {
            Emp emp=empMapper.selectByPrimaryKey(id);
            empMapper.deleteByPrimaryKey(id);
            adminMapper.deleteByPrimaryKey(emp.getAdminId());
            return 1;
        } catch(Exception e) {
            throw new TaxerExcepiton("数据库异常");
        }
    }

    @Override
    public int insert(Emp record) throws TaxerExcepiton {
        try{
            empMapper.insert(record);
            return 1;
        }catch (Exception e){
            throw new TaxerExcepiton("数据库异常");
        }
    }

    @Override
    public int insertSelective(Emp record) throws TaxerExcepiton {
        try{
            java.util.Date  date=new java.util.Date();
            java.sql.Date  data1=new java.sql.Date(date.getTime());
            Admin admin = new Admin();
            admin.setUsername("10000");
            admin.setPassword("123456");
            admin.setStatus((byte)1);
            admin.setCreated(new Date());
            admin.setUpdated(new Date());
            adminMapper.insertSelective(admin);
            Admin aa=adminMapper.selectByName("10000");
            aa.setUsername("10000"+aa.getId());
            adminMapper.updateByPrimaryKey(aa);

            record.setAdminId(aa.getId());
            record.setCreated(new Date());
            record.setUpdated(new Date());
            empMapper.insertSelective(record);
            return 1;
        }catch (Exception e){
            throw new TaxerExcepiton("数据库异常");
        }

    }
    @Override
    public Emp selectByPrimaryKey(Long id) throws TaxerExcepiton {
        try{
           Emp emp= empMapper.selectByPrimaryKey(id);
            return emp;
        }catch (Exception e){
            throw new TaxerExcepiton("数据库异常");
        }
    }

    @Override
    public int updateByPrimaryKeySelective(Emp record) throws TaxerExcepiton{
        try{
            empMapper.updateByPrimaryKeySelective(record);
            return 1;
        }catch ( Exception e){
            throw new TaxerExcepiton("数据库异常");
        }
    }

    @Override
    public int updateByPrimaryKey(Emp record) throws TaxerExcepiton {
        try{
            empMapper.updateByPrimaryKey(record);
            return 1;
        }catch (Exception e){

            throw new TaxerExcepiton("数据库异常");
        }
    }

    @Override
    public List<Emp> findAll() throws TaxerExcepiton {
        try{
           List<Emp> emps =  empMapper.findAll();
            return emps;
        }catch (Exception e){
            throw new TaxerExcepiton("数据库异常");
        }
    }

}
